package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.ds.domain.dto.LessonDto;
import com.jvyou.app.ds.domain.dto.StudentDto;
import com.jvyou.app.ds.domain.entity.Lesson;
import com.jvyou.app.ds.domain.entity.Student;
import com.jvyou.app.ds.service.LessonService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 驾校课时主表(Lesson)表控制层
 *
 * @author 橘柚
 * @since 2025-04-11 13:20:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/lesson")
public class LessonController {

    private final LessonService lessonService;

    @PostMapping("/list")
    @SaIgnore
    public Result<List<Lesson>> list(@RequestBody LessonDto lessonDto) {
        return Result.success(lessonService.listLesson(lessonDto));
    }

    @PostMapping("/page")
    @SaIgnore
    public Result<PageResult<Lesson>> page(@RequestBody LessonDto lessonDto) {
        return Result.success(lessonService.pageLesson(lessonDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("ds:lesson:get")
    public Result<Lesson> selectOne(@PathVariable Long id) {
        return Result.success(lessonService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param lesson 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("ds:lesson:insert")
    public Result<Lesson> insert(@RequestBody Lesson lesson) {
        return Result.success(lessonService.addLesson(lesson));
    }

    /**
     * 修改数据
     *
     * @param lesson 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("ds:lesson:update")
    public Result<Boolean> updateById(@RequestBody Lesson lesson) {
        boolean success = lessonService.updateById(lesson);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("ds:lesson:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = lessonService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }


    /**
     * 教练获取课时信息
     *
     * @return 单条数据
     */
    @PostMapping("/getByCoach")
    @SaCheckPermission("ds:lesson:getByCoach")
    public Result<PageResult<Lesson>> getByCoach(@RequestBody LessonDto lessonDto) {
        return Result.success(lessonService.getByCoach(lessonDto));
    }

    /**
     * 教练获取学生的课时信息
     *
     * @return 学生的课时信息
     */
    @GetMapping("/getByStudent")
    @SaCheckPermission("ds:lesson:get")
    public Result<PageResult<Lesson>> getByStudent(@ModelAttribute LessonDto lessonDto) {
        return Result.success(lessonService.getByStudent(lessonDto));
    }
}

